package com.baidu.cleaner.checker;

import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class IntChecker implements Checker {
	private static final String reg = "\\d+";
	private boolean isNull = false;
	private int min = Integer.MIN_VALUE;
	private int max = Integer.MAX_VALUE;

	public int getMin() {
		return min;
	}

	public void setMin(int min) {
		this.min = min;
	}

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}

	public boolean getIsNull() {
		return isNull;
	}

	public void setIsNull(boolean isNull) {
		this.isNull = isNull;
	}

	@Override
	public boolean verify(String value) throws Exception {
		if (!isNull && StringUtils.isBlank(value)) {
			throw new Exception("the value can not be empty");
		} else if (isNull && StringUtils.isBlank(value)) {
			return true;
		} else {
			if (!Pattern.matches(reg, value)) {
				throw new Exception("the value is not int type");
			}

			int v = Integer.parseInt(value);

			if (this.min > v || v >= this.max) {
				throw new Exception("the value is out of range");
			}

			return true;
		}
	}

	public static void main(String[] args) throws Exception {
		IntChecker ic = new IntChecker();
		//ic.setMax(10);
		//ic.setMin(1);
		ic.setIsNull(false);

		System.out.println(ic.verify("11"));
	}
}
